{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5a6ee4684a5c0265",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### this demo case requires python 3.10"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cc18ada",
   "metadata": {},
   "source": [
    "### clone代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "!git clone https://github.com/modelscope/modelscope-agent.git"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d33671c",
   "metadata": {},
   "source": [
    "### 安装特定依赖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a9d565e69358f79",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "!cd modelscope-agent && pip install -r requirements.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fc401bd",
   "metadata": {},
   "source": [
    "### 本地配置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf28ab696d3c3336",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir('modelscope-agent/examples/tools')\n",
    "\n",
    "import sys\n",
    "sys.path.append('../../')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77b83859",
   "metadata": {},
   "source": [
    "### API_KEY管理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a24ff9f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "print('请输入DASHSCOPE_API_KEY')\n",
    "os.environ['DASHSCOPE_API_KEY'] = input()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c763126a",
   "metadata": {},
   "source": [
    "### 使用agent并且选择工具"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f08750d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from modelscope_agent.agents import RolePlay\n",
    "\n",
    "role_template = '你扮演代码助手，你需要按照需求调用工具执行代码，并且提供完整代码'\n",
    "llm_config = {\n",
    "    'model': 'qwen-max', \n",
    "    'model_server': 'dashscope',\n",
    "    }\n",
    "function_list = ['code_interpreter']\n",
    "\n",
    "bot = RolePlay(function_list=function_list, llm=llm_config, instruction=role_template)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1100527a",
   "metadata": {},
   "source": [
    "### 执行agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6631842",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = bot.run(\"查看我本地python版本并返回\", remote=False, print_info=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9e8a733",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = ''\n",
    "for chunk in response:\n",
    "    text += chunk\n",
    "print(text)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
